package com.glodon.paas.activity.biz;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.glodon.paas.activity.dao.ActivityDefinitionDao;
import com.glodon.paas.activity.dao.domain.ActivityDefinitionEntity;
import com.glodon.paas.activity.dao.domain.AppInfoEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.util.asyn.AsynWorker;

@Component("activityEntityBiz")
public class ActivityDefinitionBiz {

    @Autowired
    ActivityDefinitionDao activityDefinitionDao;

    @Autowired
    AppInfoBiz            appInfoBiz;
    @Autowired
    AsynWorker            asynWorker;

    Integer               maxValue = 999999;

    public int update(ActivityDefinitionEntity entity) {
        if (entity.getAppId() == null || entity.getAppId() <= 0) {
            throw PaasServerExceptionFactory.validateException("appId is null", null);
        }

        int rt = activityDefinitionDao.updateByPrimaryKeySelective(entity);
        return rt;
    }

    /**
     * @param entity
     * @param insert : true (insert)|false(update)
     * @return
     */
    public ActivityDefinitionEntity save_xx(ActivityDefinitionEntity entity, boolean insert) {
        if (entity.getAppId() == null || entity.getAppId() <= 0) {
            throw PaasServerExceptionFactory.validateException("appId is null", null);
        }

        if (insert) {
            return insert(entity);
        }
        if (updateById(entity) == 0) {
            throw PaasServerExceptionFactory.dbException("no update", null);
        }

        ActivityDefinitionEntity db = selectById(entity.getActivityType());
        if (db == null) {
            throw PaasServerExceptionFactory.notFoundException("" + entity.getActivityType(), null);
        }
        return db;
    }

    public ActivityDefinitionEntity insert(ActivityDefinitionEntity entity) {
        Integer appId = entity.getAppId();
        Integer type = entity.getType();
        Integer modelId = entity.getModelId();

        // 检查appId
        if (appId == null) {
            throw PaasServerExceptionFactory.validateException("appId is null", null);
        }
        Map<Integer, AppInfoEntity> map = appInfoBiz.selectAllMap();
        if (!map.containsKey(appId)) {
            throw PaasServerExceptionFactory.validateException("invalid appId:" + appId, null);
        }

        // 检查modelId
        if (modelId == null) {
            throw PaasServerExceptionFactory.validateException("modelId is null", null);
        }
        if (modelId < 0 || modelId > maxValue) {
            throw PaasServerExceptionFactory.validateException("invalid modelId:" + modelId, null);
        }

        // 检查type
        if (type == null) {
            throw PaasServerExceptionFactory.validateException("type is null", null);
        }
        if (type < 0 || type > maxValue) {
            throw PaasServerExceptionFactory.validateException("invalid type:" + type, null);
        }

        Long activityType = appId * 1000000000000L + modelId * 1000000 + type;
        entity.setActivityType(activityType);

        activityDefinitionDao.insertSelective(entity);
        return entity;
    }

    public ActivityDefinitionEntity getByActivityType(Long activityType) {
        return activityDefinitionDao.getByActivityType(activityType);
    }

    public int deleteById(Long id) {
        return activityDefinitionDao.deleteByPrimaryKey(id);
    }

    private int updateById(ActivityDefinitionEntity record) {
        return activityDefinitionDao.updateByPrimaryKeySelective(record);
    }

    public ActivityDefinitionEntity selectById(Long id) {
        return activityDefinitionDao.selectByPrimaryKey(id);
    }

    public List<ActivityDefinitionEntity> selectByAppId(Integer appId) {
        return activityDefinitionDao.selectByAppId(appId);
    }
    
    public void reflashCache(){
        activityDefinitionDao.reflashCache();
    }
}
